# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=glog
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.0
pkgrel=2
pkgdesc="C++ implementation of the Google logging module (mingw-w64)"
arch=('any')
url="https://github.com/google/glog"
license=('New BSD License')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gflags"
          "${MINGW_PACKAGE_PREFIX}-libunwind")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/google/glog/archive/v${pkgver}.tar.gz"
        "${_realname}-0.4.0.patch")
sha256sums=('f28359aeba12f30d73d9e4711ef356dc842886968112162bc73002645139c39c'
            'c8ef5e7a1c2747224abee308f8fa47693962c3693587022c29e5567711fc1b61')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "../${_realname}-0.4.0.patch"
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=YES \
    "../${_realname}-${pkgver}"

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/glog/glog-targets.cmake
}
